<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="50%" @close="$emit('close')">
    <el-form ref="form" label-width="120px" :rules="rules" :model="formData">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <!-- <el-input v-model="formData.manager" /> -->
        <el-select v-model="formData.manager" style="width:100%;" placeholder="请选择部门负责人" @focus="getEmployeeSimple">
          <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" :row="2" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="add">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// // some,every,map,filter,forEach
// some 只要有一个一样的就是true,否则就是false
// every 只有所有的都一样就是true,否则就是false
// fitler 筛选 大数组筛选子数组
// map 映射 [1,2,3].map(item=>item*item) ---> [1,4,9]
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    node: {
      type: Object,
      default: () => {}// 默认值是空对象
    }
  },
  data() {
    // value就是要校验的数据
    // callback
    // 查询出来所有的部门，然后根据当前父部门的id，也就是这个父部门的所有的子部门的pid筛选出来所有的子部门 判断有没有重复
    var checkName = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      var bool
      if (this.formData.id) {
        // 说明部门名没有修改,还是原来的
        if (value === this.node.name) {
          return callback()
        }
        // 编辑 如果是编辑的话,我们点击的是儿子里面的一个,这个时候,我们就根据pid找( this.node.pid)
        bool = depts.filter(item => item.pid === this.node.pid).some(item => item.name === value)
      } else {
        // 添加 如果是添加的话,我们点击的是父节点,这个时候,就根据父节点的id来进行对比,把所有的儿子节点找出来
        bool = depts.filter(item => item.pid === this.node.id).some(item => item.name === value)
      }
      if (bool) {
        callback(new Error(`名字${value}和现有的子部门重复了`))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      formData: {
        code: '', // 部门编码，同级部门不可重复
        introduce: '', // 介绍
        manager: '', // 负责人名称=
        name: '' // 部门名称
      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      },
      peoples: [],
      title1: '新增部门'
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  // watch: {
  //   showDialog() {
  //     this.dialogVisible = this.showDialog
  //   }
  // },
  created() {
    this.dialogVisible = this.showDialog
  },
  methods: {
    async getEmployeeSimple() {
      // this.getEmployeeSimple
      const res = await getEmployeeSimple()
      console.log(res)
      this.peoples = res
    },
    add() {
      // 表单预校验
      this.$refs.form.validate(async valid => {
        if (!valid) return // 校验失败
        // console.log('校验成功')
        if (this.formData.id) {
          // 编辑保存
          await updateDepartments(this.formData)
        } else {
          await addDepartments({ ...this.formData, pid: this.node.id })
        }
        // 子向父传值
        this.$emit('add')
      })
    },
    async getData(id) {
      this.formData = await getDepartDetail(id)
    }
  }
}
</script>

<style scoped lang="less">
</style>
